using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Hematology.Data
{
	[DataObject]
	[Serializable]
	public partial class Job
	{
		#region Constants

		private const string TableName = "[dbo].[Job]";

		#endregion

		#region Properties

		public int? JobId { get; set; }
		public int? CountryId { get; set; }
		public int? RegionId { get; set; }
		public int? CityId { get; set; }
		public string FullName { get; set; }
		public string Abbreviation { get; set; }
		public bool? IsVisible { get; set; }
		public bool? IsDeleted { get; set; }
		public DateTime? CreateDate { get; set; }
		public DateTime? LastModifiedDate { get; set; }

		#endregion

		#region Methods

		#region Mapping Methods

		protected void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);

			ds.Tables[TableName].Columns.Add("JobId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("CountryId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("RegionId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("CityId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("FullName", typeof(String));
			ds.Tables[TableName].Columns.Add("Abbreviation", typeof(String));
			ds.Tables[TableName].Columns.Add("IsVisible", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("CreateDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("LastModifiedDate", typeof(DateTime));

			DataRow dr = ds.Tables[TableName].NewRow();

			dr["JobId"] = JobId ?? (object) DBNull.Value;
			dr["CountryId"] = CountryId ?? (object) DBNull.Value;
			dr["RegionId"] = RegionId ?? (object) DBNull.Value;
			dr["CityId"] = CityId ?? (object) DBNull.Value;
			dr["FullName"] = FullName ?? (object)DBNull.Value;
			dr["Abbreviation"] = Abbreviation ?? (object)DBNull.Value;
			dr["IsVisible"] = IsVisible ?? (object) DBNull.Value;
			dr["IsDeleted"] = IsDeleted ?? (object) DBNull.Value;
			dr["CreateDate"] = CreateDate ?? (object)DBNull.Value;
			dr["LastModifiedDate"] = LastModifiedDate ?? (object)DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}

		protected void MapFrom(DataRow dr)
		{
			JobId = dr["JobId"] != DBNull.Value ? Convert.ToInt32(dr["JobId"]) : JobId = null;
			CountryId = dr["CountryId"] != DBNull.Value ? Convert.ToInt32(dr["CountryId"]) : CountryId = null;
			RegionId = dr["RegionId"] != DBNull.Value ? Convert.ToInt32(dr["RegionId"]) : RegionId = null;
			CityId = dr["CityId"] != DBNull.Value ? Convert.ToInt32(dr["CityId"]) : CityId = null;
			FullName = dr["FullName"] != DBNull.Value ? Convert.ToString(dr["FullName"]) : FullName = null;
			Abbreviation = dr["Abbreviation"] != DBNull.Value ? Convert.ToString(dr["Abbreviation"]) : Abbreviation = null;
			IsVisible = dr["IsVisible"] != DBNull.Value ? Convert.ToBoolean(dr["IsVisible"]) : IsVisible = null;
			IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
			CreateDate = dr["CreateDate"] != DBNull.Value ? Convert.ToDateTime(dr["CreateDate"]) : CreateDate = null;
			LastModifiedDate = dr["LastModifiedDate"] != DBNull.Value ? Convert.ToDateTime(dr["LastModifiedDate"]) : LastModifiedDate = null;
		}

		public static List<Job> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<Job> objects = new List<Job>();

			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");
			
			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;

			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[Job] in DataSet.");

			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[Job] is empty.");

			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				Job instance = new Job();
				instance.MapFrom(dr);
				objects.Add(instance);
			}

			// Return collection.
			return objects;
		}

		#endregion

		#region CRUD Methods

		public static Job Get(Int32 jobId)
		{
			Job instance = new Job();

			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, jobId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get Job ID:{0} from Database.", jobId));
					ds.Tables[0].TableName = TableName;

					instance.MapFrom(ds.Tables[0].Rows[0]);
				}
			}
			return instance;
		}

		#region INSERT

		private void Insert(Int32? countryId, Int32? regionId, Int32? cityId, String fullName, String abbreviation, Boolean? isVisible, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_INSERT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, countryId, regionId, cityId, fullName, abbreviation, isVisible, isDeleted, createDate, lastModifiedDate))
			{
				JobId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
			}
		}

		/// <summary>
		/// Insert current Job to database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Insert(DbTransaction transaction = null)
		{
			Insert(CountryId, RegionId, CityId, FullName, Abbreviation, IsVisible, IsDeleted, CreateDate, LastModifiedDate, transaction);
		}

		#endregion

		#region UPDATE

		private static void Update(Int32? jobId, Int32? countryId, Int32? regionId, Int32? cityId, String fullName, String abbreviation, Boolean? isVisible, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@jobId"].Value = jobId;
				dbCommand.Parameters["@countryId"].Value = countryId;
				dbCommand.Parameters["@regionId"].Value = regionId;
				dbCommand.Parameters["@cityId"].Value = cityId;
				dbCommand.Parameters["@fullName"].Value = fullName;
				dbCommand.Parameters["@abbreviation"].Value = abbreviation;
				dbCommand.Parameters["@isVisible"].Value = isVisible;
				dbCommand.Parameters["@isDeleted"].Value = isDeleted;
				dbCommand.Parameters["@createDate"].Value = createDate;
				dbCommand.Parameters["@lastModifiedDate"].Value = lastModifiedDate;

				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}

		private static void Update(Int32? jobId, Int32? countryId, Int32? regionId, Int32? cityId, String fullName, String abbreviation, Boolean? isVisible, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Update(jobId, countryId, regionId, cityId, fullName, abbreviation, isVisible, isDeleted, createDate, lastModifiedDate, null);
		}

		private static void Update(Job job)
		{
			job.Update();
		}

		private static void Update(Job job, DbTransaction transaction)
		{
			job.Update(transaction);
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Update(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@jobId"].SourceColumn = "JobId";
				dbCommand.Parameters["@countryId"].SourceColumn = "CountryId";
				dbCommand.Parameters["@regionId"].SourceColumn = "RegionId";
				dbCommand.Parameters["@cityId"].SourceColumn = "CityId";
				dbCommand.Parameters["@fullName"].SourceColumn = "FullName";
				dbCommand.Parameters["@abbreviation"].SourceColumn = "Abbreviation";
				dbCommand.Parameters["@isVisible"].SourceColumn = "IsVisible";
				dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";
				dbCommand.Parameters["@createDate"].SourceColumn = "CreateDate";
				dbCommand.Parameters["@lastModifiedDate"].SourceColumn = "LastModifiedDate";

				using (DataSet ds = new DataSet())
				{
					MapTo(ds);
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		private void Update()
		{
			Update((DbTransaction)null);
		}
		#endregion


		#region DELETE

		public static void Delete(Int32? jobId, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, jobId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}

		public static void Delete(Int32? jobId)
		{
			Delete(
				jobId);
		}

		/// <summary>
		/// Delete current Job from database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Delete(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, JobId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
			JobId = null;
		}

		/// <summary>
		/// Delete current Job from database.
		/// </summary>
		public void Delete()
		{
			Delete((DbTransaction)null);
		}

		#endregion

		#region SEARCH

		public static List<Job> Search(Int32? jobId, Int32? countryId, Int32? regionId, Int32? cityId, String fullName, String abbreviation, Boolean? isVisible, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspJob_SEARCH";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, jobId, countryId, regionId, cityId, fullName, abbreviation, isVisible, isDeleted, createDate, lastModifiedDate))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		public static List<Job> Search(Job searchObject)
		{
			return Search(searchObject.JobId, searchObject.CountryId, searchObject.RegionId, searchObject.CityId, searchObject.FullName, searchObject.Abbreviation, searchObject.IsVisible, searchObject.IsDeleted, searchObject.CreateDate, searchObject.LastModifiedDate);
		}

		#endregion

		#endregion

		#endregion
	}
}

